package com.cc.controller;

import com.cc.model.EbComment;
import com.cc.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @RequestMapping("/list")
    public ModelAndView list(@RequestParam(value = "page",defaultValue = "1") int page,
                             @RequestParam(value = "pageSize",defaultValue = "3")int pageSize){
        //1.获取新闻表的第一页数据，每页三条
        List<EbComment> comments = commentService.list(page,pageSize);

        //获取最大页数
        int maxPage = commentService.getMaxPage(pageSize);

        //2.将数据（List）和视图（jsp）返回给中央控制器
        ModelAndView mv = new ModelAndView();
        //绑定数据
        mv.addObject("comments",comments);
        //绑定当前页
        mv.addObject("curPage",page);
        //b绑定最大页
        mv.addObject("maxPage",maxPage);
        //绑定视图
        mv.setViewName("manage/comment");
        return mv;
    }
    @RequestMapping("/lists")
    public ModelAndView lists(@RequestParam(value = "page",defaultValue = "1") int page,
                             @RequestParam(value = "pageSize",defaultValue = "3")int pageSize){
        //1.获取新闻表的第一页数据，每页三条
        List<EbComment> comments = commentService.list(page,pageSize);

        //获取最大页数
        int maxPage = commentService.getMaxPage(pageSize);

        //2.将数据（List）和视图（jsp）返回给中央控制器
        ModelAndView mv = new ModelAndView();
        //绑定数据
        mv.addObject("comments",comments);
        //绑定当前页
        mv.addObject("curPage",page);
        //b绑定最大页
        mv.addObject("maxPage",maxPage);
        //绑定视图
        mv.setViewName("guestbook");
        return mv;
    }



    @RequestMapping("/save_commit")
    public String saveCommit(EbComment ebComment){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String timeStr = sdf.format(date);
        ebComment.setEc_Create_time(timeStr);
        if(ebComment.getEc_nick_name()==null||
                ebComment.getEc_nick_name().equals("")||
                ebComment.getEc_nick_name().trim().length()<0||
                ebComment.getEc_content()==null||
                ebComment.getEc_content().equals("")||
                ebComment.getEc_content().trim().length()<=0||
                ebComment.getEc_Create_time()==null||
                ebComment.getEc_Create_time().equals("")||
                ebComment.getEc_Create_time().trim().length()<0){
            return "manage/manage-error";
        }
        int result = commentService.save(ebComment);
        if(result<=0){

        }

        return "index";
    }
    @RequestMapping("/delete/{id}")
    public String deleteById(@PathVariable("id") int id) {
        int result = commentService.deleteByPk(id);
        System.out.println(result);
        if(result==0){

        }
        return "manage/manage-result1";
    }

    @RequestMapping("/edit")
    public String editComment(Model model, Integer ecId){
        EbComment ebComments = commentService.selectByCondition(ecId);
        System.out.println(ebComments);
        model.addAttribute("ebComments",ebComments);
        return "manage/guestbook-modify";
    }
    @RequestMapping("/edit-result")
    public String editrResult(EbComment ebComment) {
        int result = commentService.updateById(ebComment);
        if(result<=0){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }
    @RequestMapping("/edit11")
    public String editCommentss(Model model, Integer ecId){
        EbComment ebCommentsss = commentService.selectByCondition(ecId);
        System.out.println(ebCommentsss);
        model.addAttribute("ebCommentsss",ebCommentsss);
        return "guestbook";
    }

     @RequestMapping("/index")
     public String commenttest(){
         return "guestbook";
     }


}
